<?php

namespace app\admin\controller;

use Jxlwqq\IdValidator\IdValidator;
use think\Lang;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Zhuwork extends AdminControl {

    const EXPORT_SIZE = 1000;

    public function _initialize() {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/zhuwork.lang.php');
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/admin.lang.php');
    }

    /**
     * 主客服管理
     */
    public function index() {
        $work_model = model('zhuwork');
        $condition=[];
        /**
         * 检索条件
         */
        if (!empty(input('param.search_work_name')))
        {
            $condition['name'] = array('like', "%" . input('param.search_work_name') . "%");
        }
        if (!empty(input('param.search_truename')))
        {
            $condition['truename'] = array('like', "%" . input('param.search_truename') . "%");
        }
        if (!empty(input('param.search_status')))
        {
            $status = input('param.search_status');
            if ($status == 2){
                $status=0;
            }
            $condition['status'] = $status;
        }
        $zhuwork_list = $work_model->getList($condition, "*", 10);
        $this->assign('showpage', $work_model->page_info->render());
        $this->assign('zhuwork_list', $zhuwork_list);
        $this->assign('search_work_name', trim(input('param.search_work_name')));
        $this->assign('search_truename', trim(input('param.search_truename')));
        $this->assign('search_status',trim(input('param.search_status')));

        #获取当前客服的登录信息
        $kefu_info = db('work_zhu')->where(['id'=>session('admin_id')])->find();
        $this->assign('j_info',json_encode($kefu_info));
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    /**
     * 增加主客服
     */
    public function zhuwork_add() {
        $zhuwork_model = model('zhuwork');
        if (request()->isPost()) {
            $data = [
                'zhuwork_name' => input('post.zhuwork_name'),
                'zhuwork_account' => input('post.zhuwork_account'),
                'zhuwork_password' => input('post.zhuwork_password'),
                'zhuwork_queren_password' => input('post.zhuwork_queren_password'),
                'zhuwork_status' => input('post.zhuwork_status'),
                'zhuwork_gid' => input('post.zhuwork_gid'),
                'truename'=>input('post.truename'),
                'idcard'=>input('post.idcard'),
                'sex'=>input('post.sex'),
            ];
            // $work_validate = validate('zhuwork');

            // if (!$work_validate->scene('zhuwork_add')->check($data))
            // {
            //     $this->error($work_validate->getError());
            // }

            #生成极光账号
            $post = [
                'username' => $_POST['zhuwork_account'],
                'password' => $_POST['zhuwork_password'],
                'nickname' => $_POST['zhuwork_name'],
                'avatar' => '',
                'signature' => ''
            ];
            $J_name = JmRegist($post);
            $J_info = json_decode($J_name,true);
            
            $res = $zhuwork_model->addZhuwork([
                'name'=>$data['zhuwork_name'],
                'account'=>$data['zhuwork_account'],
                'password'=>md5($data['zhuwork_password']),
                'status'=>$data['zhuwork_status'],
                'login_time'=>'',
                'login_num' => 0,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time()),
                'gid'=>$data['zhuwork_gid'],
                'truename'=>$data['truename'],
                'idcard'=>$data['idcard'],
                'sex'=>$data['sex'],
                'admin_id'=>session('admin_id'),
                'admin_name'=>session('admin_name'),
                'j_name' => $J_info[0]['username']
            ]);

            if ($res){
                $this->log(lang('ds_add') . lang('zhuwork_index_zhuwork') . '[' . input('post.zhuwork_name') . ']', 1);
                dsLayerOpenSuccess(lang('ds_common_save_succ'));
            }else{
                $this->error(lang('ds_common_save_fail'));
            }
        } else {
            //得到权限组
            $admin_model = model('admin');
            $gadmin = $admin_model->getGadminList2('gname,gid',[]);
            $zhuwork_array = [
                'zhuwork_id' => '',
                'zhuwork_name' => '',
                'zhuwork_account' => '',
                'zhuwork_password' => '',
                'zhuwork_login_time' => '',
                'zhuwork_login_num' => '',
                'zhuwork_status' => '1',
                'zhuwork_gid' => '',
                'truename'=>'',
                'idcard'=>'',
                'sex'=>'',
            ];
            $this->assign('gadmin', $gadmin);
            $this->assign('zhuwork_array', $zhuwork_array);
            return $this->fetch('form');
        }
    }

    /**
     * ajax
     */
    public function ajax()
    {
        $zhuwork_model = model('zhuwork');
        switch (input('get.branch'))
        {
            case 'check_zhuwork_name':
                $condition['name'] = input('get.zhuwork_name');
                $condition['id']=['neq',input('get.zhuwork_id')];
                $info = $zhuwork_model->checkName($condition,'id');
                if (empty($info))
                {
                    exit('true');
                }else{
                    exit('false');
                }
            case 'check_zhuwork_account':
                $condition['account'] = input('get.zhuwork_account');
                $condition['id']=['neq',input('get.zhuwork_id')];
                $zi_model = model('ziwork');
                $info2 =$zi_model->getInfo($condition);
                $info = $zhuwork_model->checkName($condition,'id');
                if (empty($info)  && empty($info2))
                {
                    exit('true');
                }else{
                    exit('false');
                }
        }
    }

    /**
     * 修改状态
     */
    public function edit_status()
    {
        $zhuwork_model = model('zhuwork');
        $condition['id'] = input('get.id');
        $data[input('get.column')] = input('get.value');
        $info = $zhuwork_model->updateWork($condition,$data);
        $this->log(lang('ds_edit') . lang('zhuwork_index_zhuwork') . '[ID:' . intval(input('get.id')) . ')', 1);
        ds_json_encode(10000, lang('ds_common_op_succ'));
    }
    /**
     * 主客服修改
     */
    public function zhuwork_edit()
    {
        $work_model = model('zhuwork');

        if (request()->isPost())
        {
            $data = [
                'id'=>input('post.id'),
                'zhuwork_name' => input('post.zhuwork_name'),
                'zhuwork_account' => input('post.zhuwork_account'),
                'zhuwork_password' => input('post.zhuwork_password'),
                'zhuwork_queren_password' => input('post.zhuwork_queren_password'),
                'zhuwork_status' => input('post.zhuwork_status'),
                'zhuwork_gid' => input('post.zhuwork_gid'),
                'truename'=>input('post.truename'),
                'idcard'=>input('post.idcard'),
                'sex'=>input('post.sex'),
            ];
            // $work_validate = validate('zhuwork');
            // if (!$work_validate->scene('zhuwork_edit')->check($data))
            // {
            //     $this->error($work_validate->getError());
            // }
            $data2 = [
                'name'=>$data['zhuwork_name'],
                'account'=>$data['zhuwork_account'],
                'status'=>$data['zhuwork_status'],
                'updated_at'=>date('Y-m-d H:i:s',time()),
                'gid'=>$data['zhuwork_gid'],
                'truename'=>$data['truename'],
                'idcard'=>$data['idcard'],
                'sex'=>$data['sex'],

            ];
            if (strlen($data['zhuwork_password']))
            {
                $data2['password']  = md5($data['zhuwork_password']);
            }
            $res = $work_model->updateWork(['id'=>input('post.id')],$data2);
            if ($res){
                $this->log(lang('ds_update') . lang('zhuwork_index_zhuwork') . '[' . input('post.zhuwork_name') . ']', 1);
                dsLayerOpenSuccess(lang('ds_common_save_succ'));
            }else{
                $this->log(lang('ds_update') . lang('zhuwork_index_zhuwork') . '[' . input('post.zhuwork_name') . ']', 0);
                $this->error(lang('ds_common_save_fail'));
            }
        }else{
            $admin_model = model('admin');
            $gadmin = $admin_model->getGadminList2('gname,gid',[]);
            $res = $work_model->getInfo(['id'=>input('param.work_id')],'*');
            if (empty($res))
            {
                $this->error(lang('param_error'));
            }
            $zhuwork_array = [
                'zhuwork_id' => $res['id'],
                'zhuwork_name' => $res['name'],
                'zhuwork_account' => $res['account'],
                'zhuwork_password' =>$res['password'],
                'zhuwork_login_time' => $res['login_time'],
                'zhuwork_login_num' => $res['login_num'],
                'zhuwork_status' => $res['status'],
                'zhuwork_gid' => $res['gid'],
                'truename'=>$res['truename'],
                'idcard'=>$res['idcard'],
                'sex'=>$res['sex'],
            ];
            $this->assign('zhuwork_array',$zhuwork_array);
            $this->assign('gadmin',$gadmin);
            return $this->fetch('edit');
        }
    }
    /**
     * 主客服删除
     */
    public function zhuwork_del()
    {
        $work_id = input('param.work_id');
        $work_id_array = ds_delete_param($work_id);
        if ($work_id_array == FALSE)
        {
            $this->log(lang('ds_del') . lang('zhuwork_index_zhuwork') . '[ID:' . $work_id . ']', 0);
            ds_json_encode(10001, lang('param_error'));
        }
        $work_model = model('zhuwork');
        $work_model->delWork(array('id'=>array('in',implode(',',$work_id_array))));
        $this->log(lang('ds_del') . lang('zhuwork_index_zhuwork') . '[ID:' . $work_id . ']', 1);
        ds_json_encode(10000, lang('ds_common_del_succ'));
    }
    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'index', 
                'text' => lang('ds_manage'),
                'url' => url('Zhuwork/index'),
            ),
            array(
                'name' => 'brand_add',
                'text' => lang('ds_add'), 
                'url' => "javascript:dsLayerOpen('".url('Zhuwork/zhuwork_add')."','".lang('ds_add')."')"
            ),
//            array(
//                'name' => 'gadmin',
//                'text' => lang('limit_gadmin'),
//                'url' => url('Admin/gadmin')
//            ),
//            array(
//                'name' => 'gadmin_add',
//                'text' => lang('admin_add_limit_gadmin'),
//                'url' => "javascript:dsLayerOpen('" . url('Admin/gadmin_add') . "','".lang('admin_add_limit_gadmin')."')"
//            ),
        );
        return $menu_array;
    }
    public function ajax_idcard()
    {
        $idcard = input('get.idcard');
        
        if ($bool = checkIdCard($idcard)){
            exit('true');
        }else{
            exit('false');
        }
    }
    
    #初始化 验证签名
    public function getImInit(){
        $user_id = input('get.user_id');
        var_dump($user_id);exit;
    }

}